<template>
  <view>
    <view class="home_invite">
      <view class="invite_text">
        <image src="https://mkservice.mk-iot.com/subFile/mk2240/LOGO.png" />
        <text>{{invitetime?'邀请你'+joinText:'邀请失效'}}</text>
      </view>
      <view class="reason" v-if="!invitetime">
        <text>原因1:此邀请的有效时间为5分钟，若超过时间后，此链接失效。</text>
        <text>原因2: 此链接仅第一个点击的用户可用。</text>
      </view>
      <button v-if="invitetime" type="primary" style="width: 40%;background-color: #1989fa" @tap="btnAgree">同意</button>
      <button type="default" plain="true" style="width: 40%;margin-top: 20rpx;" @tap="btnReturn">返回</button>
    </view>
  </view>
</template>

<script>
  import {
    getShareRecord,
    addShareRecord
  } from '@/api/system/store/shareRecord.js'
  export default {
    data() {
      return {
        invitetime: false,
        shareType: null,
        sharerId: null,
        equipmentStoreNo: null,
        userId: uni.getStorageSync("userId"),
        joinText: this.shareType == 1 ? '加入门店' : '加入控制设备'
      }
    },
    onLoad(option) {
      if (this.userId != null && this.userId != '') {
        this.shareType = option.shareType
        this.sharerId = option.sharerId
        this.equipmentStoreNo = option.equipmentStoreNo,
        this.invitetime = true
      } else {
         uni.reLaunch({
           url:"/page/login",
           })
      }
    },
    methods: {
      //同意
      btnAgree() {
        //先查询
        if (this.userId == this.sharerId) {
          this.$modal.showToast('分享者和被分享者不能是同一人')
        } else {
          const queryData={
            equipmentStoreNo:this.equipmentStoreNo,
            recipientId:this.userId
          }
          getShareRecord(queryData).then(response => {
            if (response.code == 200) {
              if (response.data != null) {
                  this.invitetime=false 
                  this.$modal.showToast(response.data.shareType==0?'你已经存在该共享设备了':
                 '你已经存在该共享门店了')
                 setTimeout(() => {
                  uni.reLaunch({
                    url:"/devicelistpkg/index/index"
                  })
                 }, 3000);
              } else {
                this.addShare(this.shareType)
              }
            }
          })
        }
      },
      //返回
      btnReturn() {
        uni.reLaunch(
        {
          url:"/devicelistpkg/index/index",
        })
      },
      //新增分享公共方法
      addShare(shareType) {
        const data = {
          equipmentStoreNo: this.equipmentStoreNo,
          sharerId: this.sharerId,
          recipientId: this.userId,
          shareType: shareType
        }
        addShareRecord(data).then(response => {
          if (response.code == 200) {
            uni.reLaunch({
              url: "/devicelistpkg/index/index",
              success: (res) => {
                if (res.errMsg = "reLaunch:ok") {
                  this.$modal.msgSuccess("加入成功");
                  this.invitetime=false  
                }
              },
            })
          } else {
            this.$modal.msgError("加入失败");
             this.invitetime=false  
          }
        })
      }
    }
  }
</script>

<style>
  page {
    background-color: #f9f9f9;
  }

  .home_invite {
    display: flex;
    flex-direction: column;
    text-align: center;
    margin-top: 100rpx;
  }

  .invite_text {
    margin-bottom: 20rpx;
  }

  .invite_text>image {
    width: 100px;
    height: 100px;
  }

  .invite_text>text {
    display: block;
    margin-top: 10rpx;
    font-size: 36rpx;
  }

  .reason {
    color: gray;
    margin: auto 10px;
  }

  .reason>text {
    display: block;
    margin-bottom: 30rpx;
  }

  .btn_invite {
    width: 400rpx;
    margin-bottom: 10px;
  }
</style>